package spark.core.scala

import org.apache.spark.{SparkContext, SparkConf}

/**
 * Created by Administrator on 2018/2/10/010.
 */
object SortWordCount {

  def main(args: Array[String]) {
    val conf = new SparkConf()
      .setAppName("SortWordCount")
      .setMaster("local")
    val sc = new SparkContext(conf)
    val lines = sc.textFile("datas/wordcount.txt")

    val  words = lines.flatMap(line => line.split(" "))
    val pairs = words.map(word =>(word,1))
    val wordcouts = pairs.reduceByKey(_+_)
    val countwords = wordcouts.map(wordcount => (wordcount._2,wordcount._1))
    val sortedcountword = countwords.sortByKey(false)
    val sortedwordcounts = sortedcountword.map(sortedcountword => ( sortedcountword._2, sortedcountword._1))
    sortedwordcounts.foreach(s => println(s._1+" "+s._2))

  }

}
